package com.TexasHoldem.util;



import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.TexasHoldem.TexasHoldemServerEx;



public class ResourceBundleConfig {
	private static final Logger logger = Logger.getLogger(TexasHoldemServerEx.class);

	private String configFileName;

	private ResourceBundle bundle;

	public static ResourceBundleConfig instance = null;
	
	public static String CONFIG_FILE_NAME = "texasholdemserver";
	
	public static ResourceBundleConfig getInstance() {
		if(instance == null)
			instance = new ResourceBundleConfig(CONFIG_FILE_NAME);
		return instance;
	}

	public ResourceBundleConfig(String name) {
		this.configFileName = name;
		
		bundle = ResourceBundle.getBundle(configFileName, Locale.getDefault());
		logger.info(toString());
	}

	@SuppressWarnings("unchecked")
	public String toString() {
		StringBuffer sBuff = new StringBuffer();
		sBuff.append("ResourceBundleConfig init from ").append(configFileName).append(".properties for RatingServer");

		Object[] keyArray = bundle.keySet().toArray();
		Arrays.sort(keyArray);
		
		for (int i = 0; i < keyArray.length; i++) {
			String key = (String)keyArray[i];
			String value = bundle.getString(key);
			sBuff.append("\n\t").append(key).append("\t = ").append(value);
		}
		
		return sBuff.toString();
	}

	public String getPropertyNotNull(String key) {
		String value = bundle.getString(key);
		if (value == null) {
			logger.warn("Can't find " + key + " define in " + configFileName + ".properties");
			return "";
		}
		return value.trim();
	}

	public String getProperty(String key) {
		return bundle.getString(key);
	}
}
